<?php
require_once('config.php');

class dbclass{
	function __construct(){
	}

	function __destruct(){}

	function __get($att){
		return $att;
	}

	function __set($att , $val){
		$this->$att = $val;
	}

	function __call($method , $p){
		if($method)
			if(is_object($p[0]))
				$this->displayObject($p[0]);
			else if(is_array($p[0]))
				$this->displayArray($p[0]);
			else
				$this->displayScalar($p[0]);
	}

	function connect(){
		global $wbDBtype , $wbDBserver , $wbDBname , $wbDBuser , $wbDBpassword , $wbDBport , $wbDBprefix;

		$this->db = new mysqli($wbDBserver , $wbDBuser , $wbDBpassword , $wbDBname);
		if (mysqli_connect_errno()){
			echo 'Error : Could not connect to database. Please try again later';
			exit;
		}
	}

	function query($query){
		if(!$query){
			echo "Please check the sql query";
			exit;
		}
		$result = $this->db->query($query);
		return $result;
	}

	/**@ $type:MYSQLI_NUM | MYSQLI_ASSOC | MYSQLI_BOTH**/
	/**return value as array for allresult
	 MYSQLI_NUM : $row[0], $row[1];  
	 MYSQLI_ASSOC : $row["Name"], $row["CountryCode"];  
	 MYSQLI_BOTH :  $row[0], $row["CountryCode"];
	**/
	function arrayResult($result , $type){
		if($type)		return $result->fetch_array($type);
	}
	
	/**return value as array for one line
	 $row[0], $row[1];  
	**/
	function rowResult($result){
		if($result)	return $result->fetch_row();
	}

	/**return value as array for one line
	 $row["Name"], $row["CountryCode"];  
	**/
	function assocResult($result){
		if($result)	return $result->fetch_assoc();
	}

	/**return value as array for one line
	 $oneresult->Name 
	 $oneresult->CountryCode; 
	**/
	function fieldResult($result){
		if($result)	return $result->fetch_field();
	}
	
	/**return value as array for one line
	 $oneresult->Name 
	 $oneresult->CountryCode; 
	**/
	function objectResult($result){
		if($result)	return $result->fetch_object();
	}

	/**return value as array for array
	 $oneresult->Name 
	 $oneresult->CountryCode; 
	**/
	function fieldsResult($result){
		if($result)	return $result->fetch_fields();
	}

	/***return field number**/
	function countFields($result){
		if($result)	return $result->field_count;
	}
	
	/***return result number**/
	function countResult($result){
		if($result)	return $result->num_rows;
	}


	function getCharset(){
		return $this->db->character_set_name();
	}
	
	function freeResult($result){
		$result -> free();
	}

	function disconnect(){
		$this->db->close();
	}

}

$warabiDb = new dbclass();
?>